package login;

import io.ClientSession;
import io.OperationResponse;

import java.util.HashMap;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import platform.AppConfig;
import protocol.ParameterType;
import protocol.ReturnCode;

/**
 * 
 * 
 * @author zheng jiang yu
 * 
 */
public class TBLogin {
	protected static final Logger log = LoggerFactory.getLogger(TBLogin.class);
	static String url = "http://tgi.tongbu.com/checkv2.aspx";

	public static void login(OperationResponse operationRequest,
			ClientSession session) {

		try {

			if (operationRequest.Parameters.size() < 2) {
				OperationResponse response = new OperationResponse(
						operationRequest.OperationCode, null, ReturnCode.No, "");
				session.SendOperationResponse(response);
			}
			String id = (String) operationRequest.Parameters
					.get(ParameterType.UserID);
			String token = (String) operationRequest.Parameters
					.get(ParameterType.UserPwd);
			String deviceID = (String) operationRequest.Parameters
					.get(ParameterType.DeviceID);
			String isGetIP = (String) operationRequest.Parameters
					.get(ParameterType.IsGetIP);
			String PhoneType = (String) operationRequest.Parameters
					.get(ParameterType.PhoneType);

			if (id.equals("zealm") && token.equals("")) {
				OperationResponse response = new OperationResponse(
						operationRequest.OperationCode, AppConfig.dicAppInfo,
						ReturnCode.GetServer, "");
				session.SendOperationResponse(response);
				return;

			}
			String body = "";
			id = "k=" + id;
			
			body = Util.sendGet(url, id);
			if (!body.equals("")) {
				if (Integer.parseInt(body) > 0) {
					body = "tbt" + body;
					operationRequest.Parameters.put(ParameterType.UserID, body);
					HashMap<Short, Object> myPrams = new HashMap<Short, Object>();
					myPrams.put(ParameterType.UserID, body);
					OperationResponse responseID = new OperationResponse(
							operationRequest.OperationCode, myPrams,
							ReturnCode.HasID, "");
					session.SendOperationResponse(responseID);
					Util.sdkReceived(operationRequest, session, body, deviceID,
							isGetIP, "tbt", PhoneType,"");

				}
			} else {
				if(!body.equals("")){
				body = Util.sendGet(url, id);
				if (Integer.parseInt(body) > 0) {
					body = "tbt" + body;
					operationRequest.Parameters.put(ParameterType.UserID, body);
					HashMap<Short, Object> myPrams = new HashMap<Short, Object>();
					myPrams.put(ParameterType.UserID, body);
					OperationResponse responseID = new OperationResponse(
							operationRequest.OperationCode, myPrams,
							ReturnCode.HasID, "");
					session.SendOperationResponse(responseID);
					Util.sdkReceived(operationRequest, session, body, deviceID,
							isGetIP, "tbt", PhoneType,"");

				}
				}
			}
		} catch (Exception e) {
			log.error("tb return have a expection ="+e.toString());

		}

	}

	public Runnable getLoginTB(OperationResponse operationResponse,
			ClientSession session) {
		class LoginTB implements Runnable {
			private OperationResponse t_operationResponse;
			private ClientSession t_session;

			public LoginTB(OperationResponse operationResponse,
					ClientSession session) {
				t_operationResponse = operationResponse;
				t_session = session;
			}

			@Override
			public void run() {
				try {
					login(t_operationResponse, t_session);
				} catch (Exception e) {
					log.info(Thread.currentThread().getName()
							+ " Login task Error!!!");
				}
			}
		}
		return new LoginTB(operationResponse, session);
	}

}
